package com.vietnam.power.admin.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.vietnam.power.constant.GeneralConstant;
import com.vietnam.power.domain.PaymentMethod;
import com.vietnam.power.domain.PaymentMethodSearchCriteria;
import com.vietnam.power.service.AppConfigurationService;

@Controller
@RequestMapping("/admin")
public class PaymentMethodController {
	private static Logger logger = Logger.getLogger(PaymentMethodController.class);
	@Autowired
	private MessageSource messageSource;

	@Autowired
	private AppConfigurationService appConfigurationService;

	@RequestMapping(value = { "/savePaymentMethod" }, method = RequestMethod.POST)
	public String savePaymentMethod(HttpServletRequest request, HttpServletResponse response, @ModelAttribute(value = "paymentMethod") PaymentMethod paymentMethod) {

		logger.debug(paymentMethod.getId());
		if (paymentMethod.getId() == null) {
			appConfigurationService.createPaymentMethod(paymentMethod);
		} else if (paymentMethod.getId() != null) {
			appConfigurationService.updatePaymentMethod(paymentMethod);
		}

		return "redirect:/admin/viewPaymentMethod.html?id=" + paymentMethod.getId();
	}

	@RequestMapping(value = { "/createPaymentMethod" }, method = RequestMethod.GET)
	public ModelAndView createPaymentMethod(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute(value = "paymentMethod") PaymentMethod paymentMethod) {
		ModelAndView mav = new ModelAndView("admin/newPaymentMethod");

		mav.addObject("paymentMethod", new PaymentMethod());
		mav.addObject("pageTitle", messageSource.getMessage("admin.payment.method.title", null, LocaleContextHolder.getLocale()));
		return mav;
	}

	@RequestMapping(value = { "/viewPaymentMethod" }, method = RequestMethod.GET)
	public ModelAndView viewPaymentMethod(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "id", required = true) int id) {
		ModelAndView mav = new ModelAndView("admin/newPaymentMethod");

		mav.addObject("paymentMethod", appConfigurationService.getPaymentMethodById(id));
		mav.addObject("mode", GeneralConstant.MODE_VIEW);
		mav.addObject("pageTitle", messageSource.getMessage("admin.payment.method.title", null, LocaleContextHolder.getLocale()));
		return mav;
	}

	@RequestMapping(value = { "/updatePaymentMethod" }, method = RequestMethod.GET)
	public ModelAndView updatePaymentMethod(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "id", required = true) int id) {
		ModelAndView mav = new ModelAndView("admin/newPaymentMethod");
		logger.debug("product group id: " + id);
		
		mav.addObject("mode", GeneralConstant.MODE_EDIT);
		mav.addObject("paymentMethod", appConfigurationService.getPaymentMethodById(id));
		mav.addObject("pageTitle", messageSource.getMessage("admin.payment.method.title", null, LocaleContextHolder.getLocale()));
		return mav;
	}

	@RequestMapping(value = { "/searchPaymentMethod" }, method = RequestMethod.GET)
	public ModelAndView searchPaymentMethod(HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView("admin/searchPaymentMethod");

		mav.addObject("searchPaymentMethod", new PaymentMethodSearchCriteria());
		mav.addObject("pageTitle", messageSource.getMessage("admin.payment.method.title", null, LocaleContextHolder.getLocale()));
		return mav;
	}

	@RequestMapping(value = { "/searchPaymentMethod" }, method = RequestMethod.POST)
	public ModelAndView searchPaymentMethod(HttpServletRequest request, HttpServletResponse response,
			@ModelAttribute(value = "searchPaymentMethod") PaymentMethodSearchCriteria s) {
		ModelAndView mav = new ModelAndView("admin/searchPaymentMethod");

		mav.addObject("searchPaymentMethod", s);
		mav.addObject("rs", appConfigurationService.searchPaymentMethod(s));
		mav.addObject("pageTitle", messageSource.getMessage("admin.payment.method.title", null, LocaleContextHolder.getLocale()));
		return mav;
	}

	@RequestMapping(value = { "/deletePaymentMethod/{id}" }, method = RequestMethod.DELETE)
	public @ResponseBody
	void deletePaymentMethod(HttpServletRequest request, HttpServletResponse response, @PathVariable(value = "id") int id) {
		appConfigurationService.deletePaymentMethod(id);
	}
}
